package view.toolbar.alert
{
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	import model.Application;
	
	import org.aswing.JComboBox;
	import org.aswing.event.InteractiveEvent;
	
	import util.Button;
	import util.Container;
	
	import view.room.Room;
	import view.toolbar.Dice;
	import view.toolbar.Dices;
	import view.toolbar.event.DiceEvent;

	public class AlertPlayerOption extends SuperAlert
	{
		private var _ui:UIAlertPlayerOption;
		
		public var dice1:Dice;
		public var dice2:Dice;
		public var dice3:Dice;
		public var dice4:Dice;
		public var dice5:Dice;
		public var dice6:Dice;
		public var diceContainer:Sprite;
		public var btnsContainer:Container;
		public var comboBox:JComboBox;
		
		private var _dices:Dices;
		private var _selectedPoint:int;
		private var _currentDiceNumber:int;
		private var _currentDiceValue:int;
		
		private var _resultDiceValue:int;
		
		private const MaxDiceNumber:int = 12;
		
		public function AlertPlayerOption()
		{
			createUI();
		}
		
		private function createUI():void
		{
			_ui = new UIAlertPlayerOption();
			addChild(_ui);
			addBtns(_ui.btnsContainer);
			
			comboBox = new JComboBox;
			comboBox.width = 40;
			comboBox.height = 20;
			comboBox.x = 190;
			comboBox.y = 145;
			
			comboBox.setListData([1,2,3,4,5,6,7,8,9,10]);
			comboBox.addEventListener(InteractiveEvent.SELECTION_CHANGED, onComboBoxChanged);
			
			addChild(comboBox);
			
			createDices();
		}
		
		private function onComboBoxChanged(e:InteractiveEvent):void{
			trace(comboBox.getSelectedItem());
		}
		
		private function createDices():void
		{
			_dices = new Dices;
			
			dice1 = new Dice;
			dice2 = new Dice;
			dice3 = new Dice;
			dice4 = new Dice;
			dice5 = new Dice;
			
			dice1.setPoint(1);
			dice2.setPoint(2);
			dice3.setPoint(3);
			dice4.setPoint(4);
			dice5.setPoint(5);
			
			dice1.canSelected = (true);
			dice2.canSelected = (true);
			dice3.canSelected = (true);
			dice4.canSelected = (true);
			dice5.canSelected = (true);
			
			dice1.addEventListener(DiceEvent.POINT_SELECTED, onSelecedDice);
			dice2.addEventListener(DiceEvent.POINT_SELECTED, onSelecedDice);
			dice3.addEventListener(DiceEvent.POINT_SELECTED, onSelecedDice);
			dice4.addEventListener(DiceEvent.POINT_SELECTED, onSelecedDice);
			dice5.addEventListener(DiceEvent.POINT_SELECTED, onSelecedDice);
			
			_ui.dice1.addChild(dice1);
			_ui.dice2.addChild(dice2);
			_ui.dice3.addChild(dice3);
			_ui.dice4.addChild(dice4);
			_ui.dice5.addChild(dice5);
			
			_dices.addDice(dice1);
			_dices.addDice(dice2);
			_dices.addDice(dice3);
			_dices.addDice(dice4);
			_dices.addDice(dice5);
			
			function onSelecedDice(e:DiceEvent):void
			{	
				_dices.active(e.target as Dice);
				
				_selectedPoint = e.point;
				comboBox.setEnabled(true);
				
				var min:int = _currentDiceNumber;
				if (_selectedPoint <= _currentDiceValue){
					min = _currentDiceNumber+1;
				}
				else{
					
				}
				
				if(min < 1) min = 1;
				
				var temp:Array = [];
				for (var i:int=min; i<=MaxDiceNumber; i++){
					temp.push(i);
				}
				comboBox.setListData(temp);
			}
		}
		
		
		private function addBtns(container:Container):void
		{
			var submitBtn:Button = new Button();
			submitBtn.addEventListener(MouseEvent.CLICK, onClick);
			container.addItem(submitBtn);
			
			function onClick(e:MouseEvent):void
			{
				Application.instance().boast(comboBox.getSelectedItem(), _selectedPoint);
				Room.instance().showWaitingWin();
			}
		}
		
		public function setCurrentResult(value:Object){
			for(var element:String in value){
				_ui.numTxt.text = value[element];
				setCurrentValue(value[element], int(element));
			}
		}
		
		private function setCurrentValue(diceNumber:int, diceValue:int):void{
			_currentDiceNumber = diceNumber;
			_currentDiceValue = diceValue;
			
			_ui.numTxt.text = String(_currentDiceNumber);
			
			while(_ui.diceContainer.numChildren > 0){
				_ui.diceContainer.removeChild(_ui.diceContainer.getChildAt(0));
			}
			
			var dice:Dice = new Dice();
			dice.setPoint(_currentDiceValue);
			_ui.diceContainer.addChild(dice);
			
			comboBox.setEnabled(false);
		}
	}
}